import { Module } from 'vuex';

const tokenModule = {
  namespaced: true,
  state: () => ({
    username: '',
    userID: '',
    token: '',
    isLogin: false
  }),
  mutations: {
    SET_TOKEN(state, payload) {
      state.username = payload.username;
      state.userID = payload.userID;
      state.token = payload.token;
      state.isLogin = true;
    },
    CLEAR_TOKEN(state) {
      state.username = '';
      state.userID = '';
      state.token = '';
      state.isLogin = false;
    }
  },
  actions: {
    saveToken({ commit }, payload) {
      commit('SET_TOKEN', payload);
    },
    removeToken({ commit }) {
      commit('CLEAR_TOKEN');
    }
  },
  getters: {
    getToken: state => state.token,
    getUsername: state => state.username,
    getUserID: state => state.userID,
    getIsLogin: state => state.isLogin
  }
};

export default tokenModule;
